# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# Author     ：skyTree
# version    ：python 3.11
# Description：工具类
"""
import hashlib
from time import time

from flask import current_app


# 递归生成树形结构
def get_tree(info_list, level, flag):
    info_dict = []
    if info_list:
        for i in info_list:
            i_dict = i.to_dict()
            if flag:
                if i.level < level:
                    i_dict['children'] = get_tree(i.children, level, flag)
            else:
                if i.level != 3:
                    i_dict['children'] = get_tree(i.children, level, flag)
            info_dict.append(i_dict)
    return info_dict


# 判断文件类型
def allow_upload(filename):
    return "." in filename and filename.split('.')[-1] in current_app.config['ALLOWED_IMG']


# 加密文件名，从而使文件名唯一
def md5_file():
    md5_project = hashlib.md5()
    md5_project.update(str(time()).encode())
    return md5_project.hexdigest()
